import json
import time

import scrapy
from scrapy import Request
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

from app import evn_
from config import BaseConfig
from spiders.items import IndustryGrowthListItem, IndustryStockListItem
from utils.blacklist import StockBlack
from utils.util import CommonUtil


class IndustryGrowthListSpider(scrapy.Spider):
    '''
    行业涨幅排行   http://quote.eastmoney.com/center/boardlist.html#industry_board
    概念排行: http://data.eastmoney.com/bkzj/gn.html
    http://push2.eastmoney.com/api/qt/clist/get?cb=jQuery112309945479961265271_1665619630348&fid=f62&po=1&pz=50&pn=1&np=1&fltt=2&invt=2&ut=b2884a393a59ad64002292a3e90d46a5&fs=m%3A90+t%3A3&fields=f12%2Cf14%2Cf2%2Cf3%2Cf62%2Cf184%2Cf66%2Cf69%2Cf72%2Cf75%2Cf78%2Cf81%2Cf84%2Cf87%2Cf204%2Cf205%2Cf124%2Cf1%2Cf13
    '''
    name = 'industry_growth_list'

    start_urls = [
        'https://47.push2.eastmoney.com/api/qt/clist/get?pn=1&pz=10&po=1&np=1&ut=bd1d9ddb04089700cf9c27f6f7426281&fltt=2&invt=2&wbp2u=3529094263470942|0|0|0|web&fid=f3&fs=m:90+t:2+f:!50&fields=f2,f3,f8,f10,f12,f14,f33,f62,f128,f104,f105,f140,f124']


    stock_url = 'https://push2.eastmoney.com/api/qt/clist/get?' \
                'fid=f62' \
                '&po=1&pz=20&pn=1&np=1' \
                '&fltt=2&invt=2&ut=b2884a393a59ad64002292a3e90d46a5' \
                '&fs=b:{0}' \
                '&fields=f12,f14,f2,f3,f62,f124'

    def __init__(self):
        self.industry_blacklist = BaseConfig.CUSTOM_INDUSTRY_BLACK
        chrome_options = Options()
        chrome_options.add_argument('--disable-gpu')
        chrome_options.add_argument('lang=zh_CN.UTF-8')
        chrome_options.add_argument('headless')  # 无头浏览器
        chrome_options.add_argument('--no-sandbox')  # 必要！！
        chrome_options.add_argument('--disable-dev-shm-usage')  # 必要！！
        prefs = {
            "profile.managed_default_content_settings.images": 2,  # 禁止加载图片
            'permissions.default.stylesheet': 2,  # 禁止加载css
        }
        chrome_options.add_experimental_option("prefs", prefs)
        self.bro = webdriver.Chrome(executable_path=evn_.CHROMEDRIVER_PATH, chrome_options=chrome_options)

    def parse(self, response):
        text = response.text
        startIndex = text.find('{')
        endIndex = text.rfind('}')
        json_str = text[startIndex:endIndex + 1]
        dic_res = json.loads(json_str)
        data_ = dic_res['data']

        klines_list = data_['diff']
        if klines_list:
            for index, klines in enumerate(klines_list, start=1):
                # 最新价格
                now_price = klines['f2']
                # 涨跌幅
                pctChg = klines['f3']
                # 换手率
                turn = klines['f8']
                # 量比
                volume_ratio = klines['f10']
                # 板块编码
                industry_code = klines['f12']
                # 板块名称
                industry_name = klines['f14']
                # 资金净流入
                main_net_inflow = klines['f62']

                if main_net_inflow < 0:
                    break
                # 领涨股票名称
                leading_stock_name = klines['f128']
                # 领涨股票代码
                leading_stock_code = klines['f140']
                # 上涨家数
                rising_nums = klines['f104']
                # 下跌家数
                decliner_nums = klines['f105']

                time_stamp = klines['f124']

                timeArray = time.localtime(time_stamp)
                otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)

                interval = CommonUtil.get_time_interval(timeArray.tm_hour, timeArray.tm_min)
                if not interval:
                    break
                if interval > '60':
                    break

                item = IndustryGrowthListItem()
                item['industry_name'] = industry_name  # '板块名称'
                item['industry_code'] = industry_code  # '板块code'
                item['now_price'] = now_price  # '最新价格'
                item['volume_ratio'] = volume_ratio  # '成交量（累计 单位：股）',
                item['pctChg'] = pctChg  # '涨跌幅',
                item['turn'] = turn  # '换手率',
                item['rising_nums'] = rising_nums  # '上涨家数',
                item['decliner_nums'] = decliner_nums  # '下跌家数',
                item['leading_stock_code'] = leading_stock_code  # '领涨股票',
                item['leading_stock_name'] = leading_stock_name  # '领涨股票',
                item['main_net_inflow'] = main_net_inflow  # '主力资金净流入',
                item['create_time'] = otherStyleTime
                item['time_interval'] = interval

                yield item
                if index > 10:
                    continue
                if industry_name in self.industry_blacklist:
                    continue
                stock_url = self.stock_url.format(industry_code)
                meta = {'industry_code': industry_code, 'industry_name': industry_name, 'time_interval': interval,
                        'create_time': otherStyleTime}
                yield Request(stock_url, callback=self.parse_stock_itme, meta=meta)

    def parse_stock_itme(self, response):
        industry_code = response.meta["industry_code"]  ##取出数据
        industry_name = response.meta["industry_name"]  ##取出数据
        time_interval = response.meta["time_interval"]  ##取出数据
        create_time = response.meta["create_time"]  ##取出数据

        text = response.text
        startIndex = text.find('{')
        endIndex = text.rfind('}')
        json_str = text[startIndex:endIndex + 1]
        dic_res = json.loads(json_str)
        data_ = dic_res['data']

        klines_list = data_['diff']
        if klines_list:
            for klines in klines_list:

                stock_name = klines['f14']
                stock_code = klines['f12']

                if StockBlack.checkInBlackList(stock_code=stock_code, stock_name=stock_name):
                    continue

                # 最新价格
                now_price = klines['f2']
                if '-' == now_price:
                    break
                if now_price < 10.00:
                    continue
                # 涨跌幅
                pctChg = klines['f3']

                # 板块编码

                # 资金净流入
                main_net_inflow = klines['f62']

                if main_net_inflow < 0:
                    break

                # 时间戳  2022-07-21 15:34:18

                item = IndustryStockListItem()
                item['industry_code'] = industry_code  # '板块名称'
                item['industry_name'] = industry_name  # '板块名称'
                item['stock_name'] = stock_name  # '板块名称'
                item['stock_code'] = stock_code  # '板块code'
                item['now_price'] = now_price  # '最新价格'
                item['pctChg'] = pctChg  # '涨跌幅',
                item['main_net_inflow'] = main_net_inflow  # '主力资金净流入',
                item['create_time'] = create_time
                item['time_interval'] = time_interval

                yield item
